package org.zecontech.extraplus.mixin;

import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.goal.GoalSelector;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.common.NeoForge;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.zecontech.extraplus.api.event.MobInitGoalEvent;

@Mixin(Mob.class)
public abstract class MixinMob {

    @Shadow
    @Final
    public GoalSelector goalSelector;

    @Shadow
    @Final
    public GoalSelector targetSelector;

    /**
     * 创建实体时触发
     * @param entityType 实体类型
     * @param level  世界
     * @param ci 回调信息
     */
    @Inject(method = "<init>", at = @At("RETURN"))
    private void init$EP(EntityType entityType, Level level, CallbackInfo ci) {
        if (level instanceof ServerLevel) {// 服务器世界
            NeoForge.EVENT_BUS.post(new MobInitGoalEvent(((Mob) (Object) this), this.goalSelector, this.targetSelector));// 发送事件
        }
    }
}
